package com.ruoyi.stock.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.stock.domain.StockHeaderSettting;
import com.ruoyi.stock.service.IStockHeaderSetttingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * 表头设置Controller
 * 
 * @author ruoyi
 * @date 2024-12-30
 */
@RestController
@RequestMapping("/stock/stockheadersettting")
@Api( tags = "表头设置")
public class StockHeaderSetttingController extends BaseController
{
    @Autowired
    private IStockHeaderSetttingService stockHeaderSetttingService;

    /**
     * 查询表头设置列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "表头设置查询")
    public TableDataInfo list(StockHeaderSettting stockHeaderSettting)
    {
        // startPage();
        List<StockHeaderSettting> list = stockHeaderSetttingService.selectStockHeaderSetttingList(stockHeaderSettting);
        return getDataTable(list);
    }

    /**
     * 导出表头设置列表
     */
    @Log(title = "表头设置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, StockHeaderSettting stockHeaderSettting)
    {
        List<StockHeaderSettting> list = stockHeaderSetttingService.selectStockHeaderSetttingList(stockHeaderSettting);
        ExcelUtil<StockHeaderSettting> util = new ExcelUtil<StockHeaderSettting>(StockHeaderSettting.class);
        util.exportExcel(response, list, "表头设置数据");
    }

    /**
     * 获取表头设置详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(stockHeaderSetttingService.selectStockHeaderSetttingById(id));
    }


    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<StockHeaderSettting> util = new ExcelUtil<StockHeaderSettting>(StockHeaderSettting.class);
        List<StockHeaderSettting> stockInquery = util.importExcel(file.getInputStream());
        for(int i=0;i<stockInquery.size();i++){
            stockInquery.get(i).setCreateTime(new Date());
            stockInquery.get(i).setCreateBy(SecurityUtils.getUsername());
            stockHeaderSetttingService.insertStockHeaderSettting(stockInquery.get(i));
        }
        //String operName = getUsername();
        //String message = userService.importUser(userList, updateSupport, operName);
        return success(stockInquery.size());
    }

    /**
     * 新增表头设置
     */
    @Log(title = "表头设置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody StockHeaderSettting stockHeaderSettting)
    {
        return toAjax(stockHeaderSetttingService.insertStockHeaderSettting(stockHeaderSettting));
    }

    /**
     * 修改表头设置
     */
    @Log(title = "表头设置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody StockHeaderSettting stockHeaderSettting)
    {
        return toAjax(stockHeaderSetttingService.updateStockHeaderSettting(stockHeaderSettting));
    }

    /**
     * 删除表头设置
     */
    @Log(title = "表头设置", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(stockHeaderSetttingService.deleteStockHeaderSetttingByIds(ids));
    }
}
